#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
	char addrstring[32];
	_input:
	if(argc != 3){
		printf("Usage : xxx.xxx.xxx.xxx portnumber\n");
		gets(addrstring);
		argc = 1;
		char* ptemp =  strtok(addrstring, " ");
		while(ptemp != NULL){
			argv[argc] = ptemp;
			ptemp  = strtok(NULL, " ");
			argc++;
		}
	goto _input;
	}
	
	int socketfd = socket(AF_INET, SOCK_STREAM, 0);
	if(socketfd == -1){
		printf("socket error\n");
		return -1;
	}
   
  	struct sockaddr_in serveraddr;
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
	if(connect(socketfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0){
		printf("connect error\n");
		return -1;
	}	
	printf("connected\n");
	printf("input : \n");
	char buf[64];
    while(1){
		fgets(buf, sizeof(buf), stdin);
		if(send(socketfd, (void*)buf, sizeof(buf), 0) < 0){
			printf("send error\n");
		}
		if(strncmp(buf, "quit", 4) == 0){
			printf("quit success\n");
			break;
		}
	}
	close(socketfd);
	return 0;
}
